from datetime import datetime

from pydantic import BaseModel

# 权限模型
class PermissionBase(BaseModel):
    name: str
    code: str
    description: str| None = None
    resource_type: str

class PermissionCreate(PermissionBase):
    pass

class PermissionUpdate(PermissionBase):
    name: str | None = None
    code: str | None = None
    resource_type: str | None = None

class PermissionResponse(PermissionBase):
    id: int
    created_at: datetime
    updated_at: datetime

    # Config内部类用于配置模型行为。
    # from_attributes = True 允许模型从对象属性中提取数据进行验证和序列化，常用于ORM对象转换
    # 添加orm_mode = True，那么就可以支持ORM对象转换 （pydantic对象 = pydantic查型类.from_orm(sqlmodel对象)）
    class Config:
        from_attributes = True
        orm_mode = True

# 菜单模型
class MenuBase(BaseModel):
    name: str
    path: str
    component: str | None = None
    icon: str | None = None
    parent_id: int | None = None
    order: int = 0
    is_active: bool = True

class MenuCreate(MenuBase):
    pass

class MenuUpdate(MenuBase):
    name: str | None = None
    path: str | None = None

class MenuResponse(MenuBase):
    id: int
    created_at: datetime
    children: list["MenuResponse"] = []

    class Config:
        from_attributes = True
        orm_mode = True

# 角色模型
class RoleBase(BaseModel):
    name: str
    code: str
    description: str | None = None
    is_active: bool = True

class RoleCreate(RoleBase):
    pass

class RoleUpdate(RoleBase):
    name: str | None = None
    code: str | None = None
    is_active: bool | None = None

class RoleResponse(RoleBase):
    id: int
    created_at: datetime
    updated_at: datetime

    class Config:
        from_attributes = True
        orm_mode = True

# 用户模型
class UserBase(BaseModel):
    username: str
    email: str
    full_name: str | None = None
    is_active: bool = True
    is_superuser: bool = False

class UserCreate(UserBase):
    password: str

class UserUpdate(UserBase):
    username: str | None = None
    email: str | None = None
    full_name: str | None = None
    is_active: bool | None = None
    is_superuser: bool | None = None

class UserResponse(UserBase):
    id: int | None = None
    created_at: datetime | None = None
    updated_at: datetime | None = None

    class Config:
        from_attributes = True
        orm_mode = True


# 令牌模型
class Token(BaseModel):
    access_token: str
    token_type: str

class TokenData(BaseModel):
    username: str | None = None
    permissions: list[str] = []

# 菜单树模型
class MenuTree(MenuResponse):
    permissions: list[PermissionResponse] = []